package com.swu.education.service;

import com.swu.education.entity.Activity;
import com.swu.education.entity.Assignment;
import com.swu.education.repository.ActivityRepository;
import com.swu.education.repository.AssignmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by nana on 2018/3/13.
 */

@Service
public class AssignmentService {
  @Autowired
  AssignmentRepository assignmentRepository;
  @Autowired
  ActivityRepository activityRepository;

  public Assignment getSingleAssignmentById(int assignmrntId){
    return assignmentRepository.findOne(assignmrntId);
  }

  public List<Assignment> getAssignmentById(int courseId){
    List<Assignment> allAssignmentListByCourseId =  assignmentRepository.findAllByCourseId(courseId);
    return allAssignmentListByCourseId;
  }
  public Assignment createAssignmentByCourseId(int courseId,String assignmentName,
                                           String description,String startTime,String endTime){
    //创建assignment类并赋值
    Assignment assignment = new Assignment();
    assignment.setCourseId(courseId);
    assignment.setAssignmentName(assignmentName);
    assignment.setDescription(description);
    assignment.setStartTime(startTime);
    assignment.setEndTime(endTime);
    //存储assignment
    assignmentRepository.save(assignment);
    return assignment;
  }

  //删除assignment
  public void deleteAssignmentById(int assignmentId){
    //删除assignment
    assignmentRepository.delete(assignmentId);
    //查找与该assignment有关的activity
    List<Activity> activities = activityRepository.findAllByAssignmentId(assignmentId);
    //删除activity
    for(int i=0;i<activities.size();i++){
      activityRepository.delete(activities.get(i).getActivityId());
    }
  }
  // 修改assignment信息
  public void modifyAssignmentInfoById(int assignmentId,String assignmentName,String description,
                                       String startTime,String endTime){
    Assignment assignment = assignmentRepository.findOne(assignmentId);
    assignment.setAssignmentName(assignmentName);
    assignment.setDescription(description);
    assignment.setStartTime(startTime);
    assignment.setEndTime(endTime);
    assignmentRepository.save(assignment);
  }
}
